package com.yndsoft.jc.utils.selectphoto;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;

import java.util.ArrayList;

import butterknife.Bind;

public class BigImageActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    @Bind(R.id.vp_big_img)
    ViewPager vp_big_img;
    @Bind(R.id.title_bar_center_tv)
    TextView title_bar_center_tv;
    @Bind(R.id.back_img)
    TextView back_img;
    @Bind(R.id.sure_iv)
    ImageView sure_iv;
    private ArrayList<String> mImgses;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_image);
        handleIntent();
        setData();
        setListener();
    }

    private void handleIntent() {
        mImgses = getIntent().getStringArrayListExtra("mImgs");
        position = getIntent().getIntExtra("position", 0);
        title_bar_center_tv.setText((position + 1) + "/" + mImgses.size());
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
        vp_big_img.setAdapter(myPagerAdapter);
        vp_big_img.setCurrentItem(position);

    }

    public void setData() {

    }

    public void setListener() {
        sure_iv.setOnClickListener(this);
        back_img.setOnClickListener(this);
        vp_big_img.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_img:
                finish();
                break;
            case R.id.sure_iv:
                Intent intent = getIntent();
                intent.putExtra("mImgUrl",mImgses.get(vp_big_img.getCurrentItem()));
                setResult(RESULT_OK,intent);
                finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        title_bar_center_tv.setText((position + 1) + "/" + mImgses.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImgses.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = new ImageView(BigImageActivity.this);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(mContext).load(mImgses.get(position)).into(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BigImageActivity.this.finish();
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}
